package vn.orderonline.com.core.business.menudrink;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vn.orderonline.com.core.business.dto.MenuDrinkDto;
import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.DrinkDao;
import vn.orderonline.core.dao.MenuDrinkDao;
import vn.orderonline.core.dao.MenuDrinkDrinkDao;
import vn.orderonline.core.dao.RestaurantMenuDrinkDao;
import vn.orderonline.core.dao.entity.MenuDrink;
import vn.orderonline.core.dao.entity.MenuDrinkDrink;
import vn.orderonline.core.dao.entity.RestaurantMenuDrink;
import vn.orderonline.core.dao.entity.RestaurantMenuDrinkId;

@Service
@Transactional
public class MenuDrinkImpl implements IMenuDrink {
	
	@Autowired
	private MenuDrinkDao menuDrinkDao;
	@Autowired
	private RestaurantMenuDrinkDao restaurantMenuDrinkDao;
	@Autowired
	private MenuDrinkDrinkDao menuDrinkDrinkDao;
	@Autowired
	private DrinkDao drinkDao;

	@Override
	public List<MenuDrinkDto> getListMenuDrinkByResId(Integer resId) {
		List<MenuDrinkDto> dtoList = new ArrayList<>();
		if (resId != null) {
			List<Object[]> objList = menuDrinkDao.getListMenuDrinkByResId(resId);
			if (!objList.isEmpty()) {
				MenuDrinkDto dto = null;
				for (Object[] obj : objList) {
					dto = new MenuDrinkDto();
					dto.setRestaurantId((Integer) obj[0]);
					dto.setRestaurantName((String) obj[1]);
					dto.setMenuDrinkId((Integer) obj[2]);
					dto.setMenuDrinkName((String) obj[3]);
					dtoList.add(dto);
				}
			}
		}
		return dtoList;
	}

	@Override
	public Boolean checkExistedMenuDrinkName(Integer resId, String menuDrinkName) {
		if (resId != null && !StringUtils.isNullOrEmpty(menuDrinkName)) {
			if (menuDrinkDao.checkExistedMenuDrinkName(resId, menuDrinkName)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void saveMenuDrink(MenuDrinkDto dto) {
		MenuDrink menuDrink = new MenuDrink();
		menuDrink.setMenuDrinkName(dto.getMenuDrinkName());
		menuDrink.setCreateTime(new Timestamp(DateUtils.now()));
		menuDrinkDao.save(menuDrink);
		
		RestaurantMenuDrink rmd = new RestaurantMenuDrink();
		rmd.setId(new RestaurantMenuDrinkId(dto.getRestaurantId(), menuDrink.getMenuDrinkId()));
		rmd.setCreateTime(new Timestamp(DateUtils.now()));
		restaurantMenuDrinkDao.save(rmd);
	}

	@Override
	public void deleteMenuDrink(Integer resId, Integer menuDrinkId) {
		if (resId != null && menuDrinkId != null) {
			RestaurantMenuDrink rmd = new RestaurantMenuDrink();
			rmd = restaurantMenuDrinkDao.find(new RestaurantMenuDrinkId(resId, menuDrinkId));
			restaurantMenuDrinkDao.delete(rmd);
			
			List<MenuDrinkDrink> mddList = menuDrinkDrinkDao.getListMenuDrinkDrinkByMenuDrinkId(menuDrinkId);
			for (MenuDrinkDrink mdd : mddList) {
				menuDrinkDrinkDao.delete(mdd);
				drinkDao.delete(drinkDao.find(mdd.getId().getDrinkId()));
			}
			
			MenuDrink md = new MenuDrink();
			md = menuDrinkDao.find(menuDrinkId);
			menuDrinkDao.delete(md);
		}
		
	}

	@Override
	public void updateMenuDrink(Integer menuDrinkId, String menuDrinkName) {
		if (menuDrinkId != null && !StringUtils.isNullOrEmpty(menuDrinkName)) {
			MenuDrink menuDrink = menuDrinkDao.find(menuDrinkId);
			menuDrink.setMenuDrinkName(menuDrinkName);
			menuDrink.setUpdatetime(new Timestamp(DateUtils.now()));
			menuDrinkDao.update(menuDrink);
		}
	}

	@Override
	public String getMenuDrinkNameByMenuDrinkId(Integer menuDrinkId) {
		if (menuDrinkId != null) {
			return menuDrinkDao.getMenuDrinkNameByMenuDrinkId(menuDrinkId);
		}
		return null;
	}

}
